//
//  Client_RF225_qPalletStockController.swift
//  Wmsmobile
//
//  Created by xuNick on 16/10/9.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class Client_RF225_qPalletStockController:  HDFounctionBaseController , HDBarcodeInputCellInputDelegate {
    //MARK: - property
    var dataArray = ["托盘条码" , "托盘"]
    var dataArrayS2 : [[String : String]]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    override var firstActiveIndexPath: IndexPath?{
        set{
        }
        get{
            return IndexPath(item: 0, section: 0)  //在这里添加需要进入界面就激活的cell的位置
        }
    }
    
    //MARK: - lifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(HDBinSpecTableviewCell.self, forCellReuseIdentifier: "HDBinSpecTableviewCell")
        self.tableView.allowsSelection = true
    }
    //MARK: - delegate
    
    func clicKReturn(_ cell: HDBarcodeInputCell) {
        if cell.codeInputView.textField.text == nil {
            self.showMessage("请输入托盘条码", success: false)
            return
        }
        self.getCollection(["piPalletCode" : self.getText(0, inSection: 0) ?? ""]   , bodyMethod: "QryPalletStock", suffix: "330", success: { (dict , collections) in
            self.setText(1, inSection: 0, text: cell.codeInputView.textField.text)
            cell.codeInputView.textField.text = nil
            cell.updateButtonState()
            self.dataArrayS2 = collections
            }) { (dict) in
                cell.state = .wrong
        }
    }
    //MARK: - private
    override func isEmpty() -> (Bool) { //判断控制器是否为空，必须要实现
        return true
    }
    //MARK: - tableviewDatasource
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return dataArray.count
        }else{
            return dataArrayS2?.count ?? 1
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath as NSIndexPath).section == 0 {  //对于普通的section，采取字典保存的方式，方便赋值取值
            if let cell = cellsDict[String(describing: indexPath)] {
                return cell
            }else{
                let position = (indexPath as NSIndexPath).section * 100 + (indexPath as NSIndexPath).row
                var cell: HDFounctionBaseCell?
                switch position {
                case 0:
                    cell = HDBarcodeInputCell()
                    (cell as! HDBarcodeInputCell).InputDelegate = self
                default:
                    cell = HDProductInfocell()
                    break
                }
                cell?.titleView.text = dataArray[(indexPath as NSIndexPath).row]
                cellsDict.updateValue(cell!, forKey: String(describing: indexPath))
                return cell!
            }
        }else{ //对于需要动态改变行数的section，仍然采用数据源管理
            let cell = tableView.dequeueReusableCell(withIdentifier: "HDBinSpecTableviewCell") as! HDBinSpecTableviewCell
            if var dict = dataArrayS2?[(indexPath as NSIndexPath).row]{
                dict.updateValue(dict["货品名称"] ?? "", forKey: "left")
                dict.updateValue(dict["规格"] ?? "", forKey: "middle")
                dict.updateValue(dict["件数"] ?? "", forKey: "right")
                cell.dict = dict
            }else{
                cell.dict = ["middle" : "无数据"]
            }
            return cell
        }
    }
    
    //MARK: - tableViewDelegate
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 1:
            let view = FounctionSectionHeaderView(headerString: "货品,规格,件数")
            return view
        default:
            return nil
        }
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 10
        case 1:
            return HDFSectionHeadrHeight
        default:
            return 0
        }
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath as NSIndexPath).section == 1 && dataArrayS2 != nil {
            let dict = self.dataArrayS2![(indexPath as NSIndexPath).row]
            let vc = SearchDetailTableViewController(info: dict)
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
}
